//
//  ReadViewController.m
//  片刻
//
//  Created by lanou3g on 15/6/3.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ReadViewController.h"
#import "MaocHeader.h"
#import "ReadModel.h"
#import <UIImageView+WebCache.h>
#import "ReadCollectionViewCell.h"
#import "DetailedViewController.h"
@interface ReadViewController ()

@property (nonatomic,retain) UICollectionView *collection; //collection属性
@property (nonatomic,retain) UICollectionViewFlowLayout *layout; //布局的属性
@property (nonatomic,retain) NSMutableArray *mainReadArr; //存放数据的数组


@end

@implementation ReadViewController
-(void)dealloc
{
    _collection = nil;
    _layout = nil;
    _mainReadArr = nil;
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.mainReadArr = [NSMutableArray array];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.naviLabel.text =@"阅读";
    [self requestData];
    [self createCollertion];
    
}
-(void)createCollertion
{
    
    _layout= [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumInteritemSpacing = 20; //两个之间间距
    _layout.minimumLineSpacing = 20; //行间距
    _layout.itemSize = CGSizeMake(100, 100);
//    _layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
    _layout.headerReferenceSize = CGSizeMake(0, 10); //头部大小
    
    _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 70, SCREEN_WIDTH-20, SCREEN_HiGHT - 70) collectionViewLayout:_layout];
    _collection.backgroundColor = [UIColor whiteColor];
    
    _collection.delegate = self;
    _collection.dataSource = self;
    [_collection registerClass:[ReadCollectionViewCell class] forCellWithReuseIdentifier:@"abc"];
    [self.view addSubview:_collection];
    
    [_layout release];
    [_collection release];
}



-(void)requestData
{
    RequesetManager *requeset = [[RequesetManager alloc] initWithRequestURL:@"http://api2.pianke.me/read/columns" requestModel:POST parameyer:nil];
    requeset.parameyerDict = [@{@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"BsdsQiiw9K9DCMVzGMTikDJxQ8auML6E82dsXj1lcWP23tBPTxRY4Pw",@"version":@"3.0.2"} mutableCopy];
    requeset.delegate = self;
    [requeset startRequest]; //开启
    
    [requeset release];
}

#pragma mark - UIRequestDelegate

-(void)requestDidFinshWithData:(NSData *)data
{
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
    NSDictionary *dict1 = dict[@"data"];
    NSArray *arr = dict1[@"list"];
    for (NSDictionary *dixt in arr) {
        ReadModel *model = [[ReadModel alloc] init];
        [model setValuesForKeysWithDictionary:dixt];
        [_mainReadArr addObject:model];
        [model release];
    }
        [self.collection reloadData];
}

-(void)requestError:(NSError *)error
{
    NSLog(@"%@",error);
}

#pragma mark - UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mainReadArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    ReadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"abc" forIndexPath:indexPath];
    ReadModel *readModel = self.mainReadArr[indexPath.row];
    cell.model = readModel;


    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailedViewController *deta = [[DetailedViewController alloc]  init];
    ReadModel *model = self.mainReadArr[indexPath.row]; //属性传值
    deta.typeID = model.type;
    [self.navigationController pushViewController:deta animated:YES];
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
